/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: RxHandlerInfo
 * @Package com.rx.core.handler
 * @Description: 处理器信息
 * @author: 陈锦韬
 * @date: 2021/6/11
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.handler;

import com.rx.core.base.RxBaseConstant;
import com.rx.core.enm.HANDLER_TYPE;
import com.rx.core.util.EmptyChecker;
import lombok.Data;

/**
 * @author: CHENJINTAO170
 * @Description: 处理器信息
 * @date: 2021/6/11
 */
@Data
public class RxHandlerInfo {
    String value;
    HANDLER_TYPE[] types;
    HANDLER_TYPE[] extypes;
    String[] keys;
    String[] exkeys;
    int order;
    Class cls;

    static int size = 0;
    public static RxHandlerInfo create(RxHandler rxHandler,Class cls){
        RxHandlerInfo rxHandlerInfo = new RxHandlerInfo();
        rxHandlerInfo.keys = EmptyChecker.isEmpty(rxHandler.keys())?new String[size]:rxHandler.keys();
        rxHandlerInfo.value = rxHandler.value();
        rxHandlerInfo.order = rxHandler.order();
        rxHandlerInfo.types = EmptyChecker.isEmpty(rxHandler.types())?new HANDLER_TYPE[size]:rxHandler.types();
        rxHandlerInfo.cls = cls;
        rxHandlerInfo.extypes = EmptyChecker.isEmpty(rxHandler.extypes())?new HANDLER_TYPE[size]:rxHandler.extypes();
        rxHandlerInfo.exkeys = EmptyChecker.isEmpty(rxHandler.exkeys())?new String[size]:rxHandler.exkeys();
        return rxHandlerInfo;
    }
    public static RxHandlerInfo create(RxHandler rxHandler){
        RxHandlerInfo rxHandlerInfo = new RxHandlerInfo();
        rxHandlerInfo.keys = EmptyChecker.isEmpty(rxHandler.keys())?new String[size]:rxHandler.keys();
        rxHandlerInfo.value = rxHandler.value();
        rxHandlerInfo.order = rxHandler.order();
        rxHandlerInfo.types = EmptyChecker.isEmpty(rxHandler.types())?new HANDLER_TYPE[size]:rxHandler.types();
        rxHandlerInfo.cls = RxHandler.class;
        rxHandlerInfo.extypes = EmptyChecker.isEmpty(rxHandler.extypes())?new HANDLER_TYPE[size]:rxHandler.extypes();
        rxHandlerInfo.exkeys = EmptyChecker.isEmpty(rxHandler.exkeys())?new String[size]:rxHandler.exkeys();
        return rxHandlerInfo;
    }
    public static RxHandlerInfo create(RxHandlerPrev rxHandler){
        RxHandlerInfo rxHandlerInfo = new RxHandlerInfo();
        rxHandlerInfo.keys = EmptyChecker.isEmpty(rxHandler.keys())?new String[size]:rxHandler.keys();
        rxHandlerInfo.value = rxHandler.value();
        rxHandlerInfo.order = rxHandler.order();
        rxHandlerInfo.types = EmptyChecker.isEmpty(rxHandler.types())?new HANDLER_TYPE[size]:rxHandler.types();
        rxHandlerInfo.cls = RxHandlerPrev.class;
        // 排除用法
        rxHandlerInfo.extypes = EmptyChecker.isEmpty(rxHandler.extypes())?new HANDLER_TYPE[size]:rxHandler.extypes();
        rxHandlerInfo.exkeys = EmptyChecker.isEmpty(rxHandler.exkeys())?new String[size]:rxHandler.exkeys();
        return rxHandlerInfo;
    }
    public static RxHandlerInfo create(RxHandlerAfter rxHandler){
        RxHandlerInfo rxHandlerInfo = new RxHandlerInfo();
        rxHandlerInfo.keys = EmptyChecker.isEmpty(rxHandler.keys())?new String[size]:rxHandler.keys();
        rxHandlerInfo.value = rxHandler.value();
        rxHandlerInfo.order = rxHandler.order();
        rxHandlerInfo.types = EmptyChecker.isEmpty(rxHandler.types())?new HANDLER_TYPE[size]:rxHandler.types();

        rxHandlerInfo.cls = RxHandlerAfter.class;
        rxHandlerInfo.extypes = EmptyChecker.isEmpty(rxHandler.extypes())?new HANDLER_TYPE[size]:rxHandler.extypes();
        rxHandlerInfo.exkeys = EmptyChecker.isEmpty(rxHandler.exkeys())?new String[size]:rxHandler.exkeys();
        return rxHandlerInfo;
    }
    private boolean keyNeedOver(){
        if(EmptyChecker.isEmpty(keys)){
            return true;
        }
        if (keys.length == 1 && RxBaseConstant.DEFAULT_URL_KEY.equalsIgnoreCase(keys[0])){
            return true;
        }
        return false;
    }
    private boolean keyCanOver(RxHandler handler){
        if(EmptyChecker.isEmpty(handler.keys())){
            return false;
        }
        if (handler.keys().length == 1 && RxBaseConstant.DEFAULT_URL_KEY.equalsIgnoreCase(handler.keys()[0])){
            return false;
        }
        return true;
    }
    private boolean typeNeedOver(){
        if(EmptyChecker.isEmpty(types)){
            return true;
        }
        if (types.length == 1 && HANDLER_TYPE.NONE.equals(types[0])){
            return true;
        }
        return false;
    }
    private boolean typeCanOver(RxHandler handler){
        if(EmptyChecker.isEmpty(handler.types())){
            return false;
        }
        if (handler.types().length == 1 && HANDLER_TYPE.NONE.equals(handler.types()[0])){
            return false;
        }
        return true;
    }
    public void overValue(RxHandler handler){
        if (EmptyChecker.isEmpty(handler)) {
            return;
        }
        if (keyNeedOver()&&keyCanOver(handler)){
            keys = handler.keys();
        }
        if (EmptyChecker.isEmpty(exkeys) && EmptyChecker.notEmpty(handler.exkeys())){
            exkeys = handler.exkeys();
        }
        if (typeNeedOver()&&typeCanOver(handler)){
            types = handler.types();
        }
        if (EmptyChecker.isEmpty(extypes) && EmptyChecker.notEmpty(handler.extypes())){
            extypes = handler.extypes();
        }
    }
}
